import {startScore} from '@/api/assessment'
import useGlobalAPI from '@/hooks/useGlobalAPI'
import {
  toMyPerformance,
  toPhoneMyPerformance,
  toPhoneScoreBySelf,
  toPhoneSetTarget,
  toPhoneSetTargetByLeader,
  toScoreByLeader,
  toScoreBySelf,
  toSetTarget,
  toSetTargetByLeader
} from '@/libs/pageRedirect'
import {checkIsPhone} from '@/libs/toolkit'
import {computed, defineComponent, PropType} from 'vue'

export const PhoneBtnsByProgressStep = defineComponent({
  name: 'PhoneBtnsByProgressStep',
  props: {
    groupId: {
      type: Number,
      required: true
    },
    nowRunningByLoginuser: {
      type: Object as PropType<MyPm.PmProgress>
    },
    nowRunningProgress: {
      type: Array as PropType<MyPm.PmProgress[]>
    },
    /** 当前绩效所属人员 */
    owner: {
      type: Object as PropType<User.Info>,
      required: true
    },
    ownerPm: {
      type: Object as PropType<MyPm.Pm>,
      required: true
    }
  },
  emits: {
    rejectProgress: (): boolean => true,
    targetConfirm: (): boolean => true,
    leaderTargetConfirm: (): boolean => true,
    managerRebut: (): boolean => true,
    pmTurnover: (): boolean => true
  },
  setup(props, {emit}) {
    const {store} = useGlobalAPI()

    /**登陆者信息 */
    const loginUserInfo = computed(() => store.getters['dingding/ddUserInfo'])

    // 当前正在进行中的流程的step
    const stepByNowRunningProgress = computed(
      () =>
        Array.from(new Set((props.nowRunningProgress || []).map(item => item.pmProgressStep)))[0]
    )

    // 当前正在进行中的流程的stageCode
    const stageCodeByNowRunningProgress = computed(
      () => Array.from(new Set((props.nowRunningProgress || []).map(item => item.pmStageCode)))[0]
    )

    const userType = computed<
      'self' | 'selfmanager' | 'maker' | 'makermanager' | 'manager' | undefined
    >(() => {
      // 1. 绩效操作者 maker
      // 2. 又是绩效操作者 又是管理员  makerManager
      // 4. 绩效所属者 self
      // 5. 又是绩效所属者 又是管理员 selfManager
      // 3. 管理员 manager
      let userIdsBynowRunningProgress = props.nowRunningProgress
        ? props.nowRunningProgress.map(item => item.makeUserDDId)
        : []

      if (userIdsBynowRunningProgress.includes(props.nowRunningByLoginuser?.makeUserDDId || '')) {
        if (loginUserInfo.value.roleLevel < 3) {
          //登陆者是绩效操作人 登陆者是管理员时
          return 'makermanager'
        } else {
          // 登陆者是绩效操作人
          return 'maker'
        }
      } else if (loginUserInfo.value.userId == props.nowRunningByLoginuser?.makeUserDDId) {
        if (loginUserInfo.value.roleLevel < 3) {
          //  登陆者是绩效所属者 登陆者是管理员时
          return 'selfmanager'
        } else {
          // 登陆者是绩效所属者
          return 'self'
        }
      } else if (loginUserInfo.value.roleLevel < 3) {
        // 登陆者是管理员时
        return 'manager'
      } else {
        return undefined
      }
    })

    function handleBtnClick(data: {type: 'setTarget' | 'submit' | 'revocation' | 'a'}) {}

    /**目标制定 - 制定目标 */
    function handleToSetTargetClick() {
      let isPhone = checkIsPhone()
      if (isPhone) {
        props.nowRunningByLoginuser?.pmId &&
          props.groupId &&
          props.nowRunningByLoginuser?.pmProgressId &&
          toPhoneSetTarget({
            pmId: props.nowRunningByLoginuser?.pmId,
            groupId: props.groupId,
            progressId: props.nowRunningByLoginuser?.pmProgressId,
            userDDId: props.owner.userId
          })
      } else {
        props.nowRunningByLoginuser?.pmId &&
          props.groupId &&
          props.nowRunningByLoginuser?.pmProgressId &&
          toSetTarget({
            pmId: props.nowRunningByLoginuser?.pmId,
            groupId: props.groupId,
            progressId: props.nowRunningByLoginuser?.pmProgressId,
            userDDId: props.owner.userId
          })
      }
    }

    /**目标制定 - 确认 */
    function handleTargetConfirmClick() {
      emit('targetConfirm')
    }

    /**目标确认 - 同意 */
    function handleTargetByLeaderConfirm() {
      emit('leaderTargetConfirm')
    }

    /**目标确认 - 调整目标 */
    function handleToSetTargetByLeaderClick() {
      let isPhone = checkIsPhone()

      if (isPhone) {
        props.nowRunningByLoginuser &&
          props.groupId &&
          toPhoneSetTargetByLeader({
            pmId: props.nowRunningByLoginuser.pmId,
            groupId: props.groupId,
            progressId: props.nowRunningByLoginuser.pmProgressId,
            userDDId: props.owner.userId
          })
      } else {
        props.nowRunningByLoginuser &&
          props.groupId &&
          toSetTargetByLeader({
            pmId: props.nowRunningByLoginuser.pmId,
            groupId: props.groupId,
            progressId: props.nowRunningByLoginuser.pmProgressId,
            userDDId: props.owner.userId
          })
      }
    }

    /**目标确认 - 驳回 */
    function handleRejectProgressClick() {
      emit('rejectProgress')
    }

    /**自评 */
    function handleScoreBySelfClick() {
      if (!props.nowRunningByLoginuser) return
      if (!props.groupId) return
      let isPhone = checkIsPhone()
      if (isPhone) {
        toPhoneScoreBySelf({
          ownerDDId: props.owner.userId,
          defaultPmId: props.nowRunningByLoginuser.pmId,
          defaultGroupId: props.groupId,
          ownerPmTitle: props.ownerPm.pmFormName
        })
      } else {
        toScoreBySelf({
          ownerDDId: props.owner.userId,
          defaultPmId: props.nowRunningByLoginuser.pmId,
          defaultGroupId: props.groupId,
          ownerPmTitle: props.ownerPm.pmFormName
        })
      }
    }

    /**主管评分 */
    function handleScoreByLeaderClick() {
      if (!props.nowRunningByLoginuser) return
      if (!props.groupId) return
      toScoreByLeader({
        ownerDDId: props.owner.userId,
        defaultPmId: props.nowRunningByLoginuser.pmId,
        defaultGroupId: props.groupId,
        ownerPmTitle: props.ownerPm.pmFormName
      })
    }

    /**转交绩效 */
    function pmTurnover() {
      emit('pmTurnover')
    }

    function handleStartScoreClick() {
      startScore({
        pmFormId: props.ownerPm.pmFormId,
        startUserDDId: loginUserInfo.value.userId,
        pmGroupIdList: '',
        userDDList: props.owner.userId
      })
        .then(res => {
          let isPhone = checkIsPhone()
          if (isPhone) {
            toPhoneMyPerformance()
          } else {
            toMyPerformance()
          }
        })
        .catch(err => {})
    }

    return () => {
      const agreeBtn = (
        <a-button
          class='bg-primary text-white'
          type='primary'
          onClick={handleTargetByLeaderConfirm}
        >
          同意
        </a-button>
      )

      const setTargetBtn = (
        <a-button class='bg-primary text-white' type='primary' onClick={handleToSetTargetClick}>
          制定目标
        </a-button>
      )

      const setTargetByLeaderBtn = (
        <a-button onClick={handleToSetTargetByLeaderClick}>调整目标</a-button>
      )

      const scoreBySelfBtn = (
        <a-button class='bg-primary text-white' type='primary' onClick={handleScoreBySelfClick}>
          评分
        </a-button>
      )
      const scoreByLeaderBtn = (
        <a-button class='bg-primary text-white' type='primary' onClick={handleScoreByLeaderClick}>
          评分
        </a-button>
      )

      const turnoverBtn = <a-button onClick={pmTurnover}>转交</a-button>

      const rejectProgressBtn = <a-button onClick={handleRejectProgressClick}>驳回</a-button>

      const startScoreBtn = (
        <a-button class='bg-primary text-white' type='primaty' onClick={handleStartScoreClick}>
          发起评分
        </a-button>
      )

      const submitBtn = (
        <a-button class='bg-primary text-white' type='primary' onClick={handleTargetConfirmClick}>
          提交
        </a-button>
      )

      switch (stepByNowRunningProgress.value) {
        case 1:
          switch (userType.value) {
            case 'self':
              return []
            case 'maker':
              return [setTargetBtn, submitBtn]
            case 'manager':
              return [turnoverBtn]
            default:
              return []
          }
        case 2:
          switch (userType.value) {
            case 'self':
              return null
            case 'maker':
              return [agreeBtn, setTargetByLeaderBtn, turnoverBtn, rejectProgressBtn]
            case 'manager':
              return [turnoverBtn, rejectProgressBtn]
            default:
              return []
          }
        case 3:
          switch (userType.value) {
            case 'self':
              return []
            case 'maker':
              return []
            case 'manager':
              return [startScoreBtn, rejectProgressBtn]
            default:
              return []
          }
        case 4:
          switch (userType.value) {
            case 'self':
              return []
            case 'maker':
              return [scoreBySelfBtn]
            case 'manager':
              return [turnoverBtn, rejectProgressBtn]
            default:
              return []
          }
        case 6:
          switch (userType.value) {
            case 'self':
              return []
            case 'maker':
              return [scoreByLeaderBtn, turnoverBtn, rejectProgressBtn]
            case 'manager':
              return [turnoverBtn, rejectProgressBtn]
            default:
              return []
          }
        case 7:
          if (stageCodeByNowRunningProgress.value === '0018') {
            switch (userType.value) {
              case 'self':
                return []
              case 'maker':
                return [scoreByLeaderBtn, turnoverBtn, rejectProgressBtn]
              case 'manager':
                return [turnoverBtn, rejectProgressBtn]
              default:
                return []
            }
          }
        default:
          switch (userType.value) {
            case 'self':
              return []
            case 'maker':
              return []
            case 'manager':
              return []
            default:
              return []
          }
      }
    }
  }
})
